package com.wuyou.gen;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.*;

public class Test {

    private static final String URL = "jdbc:mysql://58.246.191.134:30222/cy_business?serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "7044ypaKxB2ujycO";


    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir("D:/workspace_generation/src/main/java"); // 生成到项目的 src/main/java 目录
        globalConfig.setAuthor("张宇航");
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(true); // 生成 Swagger 注解
        globalConfig.setIdType(IdType.AUTO); // 自动生成主键
        globalConfig.setFileOverride(true);
        generator.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(URL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername(USERNAME);
        dataSourceConfig.setPassword(PASSWORD);
        generator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.fourport.cy")
                .setModuleName("business")
                .setController("controller")
                .setService("service")
                .setServiceImpl("impl")
                .setEntity("entity")
                .setMapper("mapper")
                .setXml("mapper");
        generator.setPackageInfo(packageConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); // 字段生成策略
        strategyConfig.setEntityLombokModel(true); // 启用 Lombok
        strategyConfig.setRestControllerStyle(true); // 使用 RESTful 风格的 Controller
        strategyConfig.setInclude("enterprise", "enterprise_contact", "fee", "inspect_order", "inspect_order_cost", "inspect_order_cost_files", "inspect_order_ext", "inspect_order_files", "inspect_order_msg", "inspect_order_status", "staff"); // 生成指定表
        generator.setStrategy(strategyConfig);
// 自定义模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig
                .setEntity("templates/entity.java")
                .setController("templates/controller.java")
                .setService("templates/service.java")
                .setServiceImpl("templates/serviceImpl.java")
                .setMapper("templates/mapper.java")
                .setXml("templates/mapper.xml");
        generator.setTemplate(templateConfig);
        // 设置模板引擎
        generator.setTemplateEngine(new FreemarkerTemplateEngine());

        // 执行生成
        generator.execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
